package cn.liyunqi.ttv.web.common;

import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;

public class RequestMapArgumentResolver implements HandlerMethodArgumentResolver {

    public static Logger log = Logger.getLogger(RequestMapArgumentResolver.class);


    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        log.info("request封装判断===============");

        //如果参数类型是User并且有CurrentUser注解则支持
        if (methodParameter.getParameterType().isAssignableFrom(RequestMap.class) ) {
            return true;
        }
        return false;
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        log.info("request封装访问===============");

        if(methodParameter.getParameterType() != null
                && methodParameter.getParameterType().equals(RequestMap.class)){

            //判断controller方法参数有没有写当前用户，如果有，这里返回即可，通常我们从session里面取出来

            HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
            Object requestMap = new RequestMap(request);
            return requestMap;

        }
        return null;
    }
}
